<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\manual\controller;

use think\Controller;
use think\Db;

class Data extends Base
{
    
    public function car() {
		
		$trans = [
			'type' => [
				11 => 4,
				22 => 2,
				33 => 5,
				44 => 3,
			],
			'car' => [
				1=>2,
				2=>3,
				3=>9,
				4=>4,
				5=>10,
				6=>6,
				7=>5,
			]
			
		];
		
		$cartypes = Db::name('car_type')->select();
		$types = [];
		$i = 0;
		foreach($cartypes AS $t) {
			/*if(!$types[$t['type']][$t['car_type']][$t['seats']][$t['chartered_hour']]) {
				$types[$t['type']][$t['car_type']][$t['seats']][$t['chartered_hour']] = $t['car_type_id'];
				$i++;
			} else {
//				dump($t['type']);
//				dump($t['car_type']);
//				dump($t['seats']);
//				dump($t['chartered_hour']);
//				dump($types[$t['type']][$t['car_type']][$t['seats']][$t['chartered_hour']]);
			}*/
			$types[$t['type']][$t['car_type']][$t['seats']][$t['chartered_hour']] = $t['car_type_id'];
			
		}
//		dump($i);
//		halt($types);
		
		
		#<option value="1">优选型</option><option value="2">舒适型</option><option value="3">商务型</option><option value="4">豪华型</option><option value="5">电动车</option><option value="6">小巴车</option><option value="7">大巴车</option></select>
		$list = Db::name('car')->alias('c')->join('car_model cm','cm.id = c.temp_model')->field('c.car_id AS car_id,temp_type,temp_car_type,temp_seat,temp_hour,cm.initiate_hour As temp_hour')->select();
//		$list = Db::name('car')->field('car_id,temp_type,temp_car_type,temp_seat,temp_hour')->select();
		
		foreach($list AS $c) {
			
			//类型对比halt
//			halt($c);
//			$c['temp_type'] = $trans['type'][$c['temp_type']];
//			$c['temp_car_type'] = $trans['car'][$c['temp_car_type']];
			$c['car_type_id'] = (int)$types[(int)$c['temp_type']][(int)$c['temp_car_type']][(int)$c['temp_seat']][(int)$c['temp_hour']];
			
//			$rslt = Db::name('car')->where('car_id',$c['car_id'])->fetchSql(1)->update($c);
			$c['car_type_id'] && $rslt = Db::name('car')->where('car_id',$c['car_id'])->fetchSql(0)->update(['car_type_id'=>$c['car_type_id'], 'temp_hour'=>$c['temp_hour'] ]);
//			halt($rslt);
			
		}
		
		return 'OK';
	}
	
	
	public function cartype() {
		
		$cartypes = Db::name('car_type')->select();
		foreach($cartypes AS $t) {
			$data = [];
			
//			$car_type_name = config('base.types')[$t['type']].config('base.car_types')[$t['car_type']];
//			if(in_array($t['car_type'],[5,6,7])) {
//				$car_type_name .= "({$t['seats']}座)";
//			}
//			if($t['chartered_hour']) {
//				$car_type_name .= "-{$t['chartered_hour']}小时";
//			}
//			
//			Db::name('car_type')->where('car_type_id',$t['car_type_id'])->setField('car_type_name',$car_type_name);
//			Db::name('order')->where('car_type_id',)->
			
		}
		
		return 'Over';
		
	}
	
	public function refuel() {
		
		/*$csv = file_get_contents(ROOT_PATH.'doc/refuel.csv');
		$data = explode(PHP_EOL,$csv);
		
		$all = $arr = [];
		
		foreach($data AS $d) {
			$arr = explode(',',$d);
			
			$all[] = [
				'car_number' => $arr[0],
				'driver_name' => $arr[1],
				'label' => (int)$arr[2],
				'litre' => $arr[3],
				'price' => $arr[4],
				'refuel_date' => date('Y-m-d',strtotime($arr[5])),
				'refuel_add' => $arr[6],
				'gas_station' => $arr[7],
				'km' => $arr[10],
			];
			
		}
		
		$rslt = Db::name('refuel_record')->insertAll($all);
		
		halt($rslt);*/
		
		//车辆查ID
		/*$list = Db::name('refuel_record')->distinct(true)->field('car_number')->fetchSql(0)->column('car_number');
		$list = array_unique(array_filter($list));
		$numbers = Db::name('car')->whereIn('number',$list)->column('number','car_id');
		foreach($numbers AS $id=>$n) {
			$rslt = Db::name('refuel_record')->whereLike('car_number',$n)->fetchSql(0)->setField('car_id',$id);
//			halt($rslt);
		}*/
		//司机名
		/*$list = Db::name('refuel_record')->distinct(true)->field('driver_name')->fetchSql(0)->column('driver_name');
		$list = array_unique(array_filter($list));
		$numbers = Db::name('driver')->whereIn('name',$list)->column('name','driver_id');
		foreach($numbers AS $id=>$n) {
			$rslt = Db::name('refuel_record')->whereLike('driver_name',$n)->fetchSql(0)->setField('driver_id',$id);
//			halt($rslt);
		}*/
		
		//改公里幅度
		/*$list = Db::name('refuel_record')->distinct(true)->field('car_id')->fetchSql(0)->column('car_id');
		foreach($list AS $id) {
			//公里数大于0且大于一次加油
			$refuel = Db::name('refuel_record')->where('car_id',$id)->where('km','>',0)->order('km')->select();
//			halt($refuel);
			if(count($refuel) > 1) {
				
				foreach($refuel AS $i => $r) {
					if($i>0) {
						$rslt = Db::name('refuel_record')->where('refuel_record_id',$r['refuel_record_id'])->fetchSql(0)->setField('km_add',(int)($r['km']-$prev['km']) );
//						dump($r['km']);
//						dump($prev['km']);
//						halt($rslt);
					}
					$prev = $r;
				}
			}
			
//			halt($refuel);
		}*/
		
		
		halt('over');
		
	}
    
}
